﻿using AmazonMODEL;
using MODEL;
using SysCommon.Log;
using SysCommon.WinServices;
using SysController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Xml;

namespace AmazonMWS
{
    partial class OrdersServices : ServiceBase, IServiceController
    {
        public OrdersServices()
        {
            InitializeComponent();
        }

        public void Start()
        {
            SystemController.GetAmazonEnvelope();
            Log.Write("订单服务开启Start");
            GetOrderList_Elapsed(null, null);
            GetOrderDetailsList_Elapsed(null, null);
        }

        #region 2015-11-08 下载订单 -  void GetOrderList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-08 下载订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetOrderList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                List<OrderModel> list = OrdersController.ListOrders(appkey);
                if (list == null || list.Count < 1)
                {
                    Log.Write(string.Format("{0}-{1} 没有订单", appkey.SiteID, appkey.AccountInfoName));
                    continue;
                }
                Log.Write(string.Format("{0}-{1} 有{2}订单", appkey.SiteID, appkey.AccountInfoName, list.Count));
                // SysCommon.Email.SendEmail("valeb2@sina.com", "Amazon订单通知", string.Format("{0}帐号【{1}】条订单", appkey.AccountInfoName, list.Count));
                bool IsAgain = true;
            Again: int row = SystemController.AddAmazonOrders(list.Select(s => s.Order).ToList());
                if (row < 1)
                {
                    if (IsAgain)
                    {
                        Log.Write(string.Format("添加订单信息异常:【{0}】", row));
                        IsAgain = false;
                        goto Again;
                    }
                    Log.Write(string.Format("添加订单信息异常重试:【{0}】", row));
                    continue;
                }
                else
                {
                    Log.Write("添加订单信息成功！");
                    bool IsAgainAddress = true;
                AgainAddress: int rowAddress = SystemController.AddAmazonOrdersShippingAddress(list.Select(s => s.ShippingAddress).ToList());
                    if (rowAddress < 1)
                    {
                        Log.Write(string.Format("添加订单邮寄地址信息异常:【{0}】", row));
                        if (IsAgainAddress)
                        {
                            IsAgainAddress = false;
                            goto AgainAddress;
                        }
                        Log.Write(string.Format("添加订单邮寄地址信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单邮寄地址信息成功！");
                    }
                    bool IsAgainTotal = true;
                AgainTotal: int rowTotal = SystemController.AddAmazonOrdersTotal(list.Select(s => s.Total).ToList());
                    if (rowTotal < 1)
                    {
                        if (IsAgainTotal)
                        {
                            IsAgainTotal = false;
                            goto AgainTotal;
                        }
                        Log.Write(string.Format("添加订单金额信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单邮金额信息成功！");
                    }
                }
            }
        }
        #endregion

        #region 2015-11-08 下载订单明细 - void GetOrderDetailsList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-08 下载订单明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetOrderDetailsList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                List<AmazonOrder> list = SystemController.GetAmazonOrders(appkey);
                if (list.Count < 1 || list == null)
                {
                    Log.Write(string.Format("{0} 没有要下载订单明细的订单", appkey.AccountInfoName));
                    continue;
                }
                Log.Write(string.Format("{0} 有{1}订单明细", appkey.AccountInfoName, list.Count));
                List<AmazonOrder> listUpdate = new List<AmazonOrder>();
                foreach (var item in list)
                {
                    List<AmazonOrdersDetail> detailList = OrdersController.ListOrderItems(appkey, item.AmazonOrderId);
                    if (detailList == null || detailList.Count < 1)
                    {
                        Log.Write(string.Format("{0} 没有订单明细", appkey.AccountInfoName));
                        continue;
                    }
                    bool IsAgain = true;
                Again: int row = SystemController.AddAmazonOrdersDetails(detailList);
                    if (row < 1)
                    {
                        //TODO:
                        if (IsAgain)
                        {
                            Log.Write(string.Format("添加订单明细信息异常:【{0}】", row));
                            IsAgain = false;
                            goto Again;
                        }
                        Log.Write(string.Format("添加订单明细信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单明细信息成功！");
                        item.IsDetails = true;
                        item.UpdateTime = DateTime.Now;
                        item.UpdateUser = "GetOrderDetails";

                        listUpdate.Add(item);
                    }
                }
                bool IsAgainDetails = true;
            AgainDetails: int count = SystemController.ModifyAmazonOrders(listUpdate);
                if (count < 1)
                {
                    if (IsAgainDetails)
                    {
                        IsAgainDetails = false;
                        goto AgainDetails;
                    }
                }
            }
        }
        #endregion

        #region 2015-11-14  创建订单追踪号信息 - void CreateOrderFulfillment_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-14 创建订单追踪号信息
        /// </summary> 
        private void CreateOrderFulfillment_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                FeedsInfo model = OrdersController.OrderFulfillment(appkey);
                if (model == null)
                {
                    Log.Write("没有要创建订单追踪号信息");
                    continue;
                }
                Log.Write("创建订单追踪号信息");
                bool IsAgain = true;
            Again: int row = 1;//SystemController.AddFeedsInfo(model);
                if (row < 1)
                {
                    if (IsAgain)
                    {
                        IsAgain = false;
                        goto Again;
                    }
                    continue;
                }
                else
                {
                    Log.Write(string.Format("订单上传追踪号信息创建{0}成功！", row));
                }

            }
        }
        #endregion

        private void FollowOnOffline_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int CurrentHour = DateTime.Now.Hour;
            foreach (var appkey in SystemController.RunAccount)
            {
                switch (CurrentHour)
                {
                    case 9:
                        try
                        {
                            Log.Write("跟卖下线");
                            FeedsController.SubmitFeed(appkey, "_POST_FLAT_FILE_PRICEANDQUANTITYONLY _UPDATE_DATA_", @"D:\Doc\跟卖下线0.txt");
                        }
                        catch (Exception ex)
                        {

                            Log.WriteError(ex, "跟卖下线异常");
                        }
                        break;
                    case 18:
                        try
                        {
                            Log.Write("跟卖上线");
                            FeedsController.SubmitFeed(appkey, "_POST_FLAT_FILE_PRICEANDQUANTITYONLY _UPDATE_DATA_", @"D:\Doc\跟卖上线100.txt");
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex, "跟卖上线异常");
                        }

                        break;
                    default:
                        break;
                }
            }
        }
    }
}
